package cn.com.open.itembank.mapper;

import cn.com.open.itembank.entity.*;
import org.apache.ibatis.annotations.*;


/**
 * Created by lucky on 2017/9/1.
 */
@Mapper
public interface QuestionWriteMapper {
    /**
     * 新增试题应用类别关系表.
     *
     * @param model 试题应用类别关系
     * @return the int
     */
    @Insert("INSERT INTO QuestionCategoryRelation (QuestionID,QuestionCategoryID) VALUES (#{questionId},#{questionCategoryId})")
    int insertQuestionCategoryRelation(QuestionCategoryRelation model);

    /**
     * 删除指定试题id的试题应用类别关系.
     *
     * @param id 试题id
     * @return the int
     */
    @Delete("DELETE FROM QuestionCategoryRelation WHERE QuestionID=#{id}")
    int deleteQuestionCategoryRelation(String id);

    /**
     * 新增试题内容信息.
     *
     * @param model 试题内容
     * @return the int
     */
    @Insert("INSERT INTO QuestionContent (QuestionID,ContentXml,ContentJson) VALUES (#{questionId},#{contentXml},#{contentJson})")
    int insertQuestionContent(QuestionContent model);

    /**
     * 更新试题内容信息的json信息.
     *
     * @param id   试题id
     * @param json 试题json
     * @return the int
     */
    @Update("UPDATE QuestionContent SET ContentJson=#{json} WHERE QuestionID=#{id}")
    int updateQuestionContentJson(@Param("id") String id, @Param("json") String json);

    /**
     * 新增试题使用关系表.
     *
     * @param model the model
     * @return the int
     */
    @Insert("INSERT INTO QuestionUseRelation (QuestionID,ItemBankID,AppID,CreateOrgID) VALUES (#{questionId},#{itemBankId},#{appId},#{createOrgId})")
    int insertQuestionUseRelation(QuestionUseRelation model);

    /**
     * 新增试题章节关系表.
     *
     * @param model the model
     * @return the int
     */
    @Insert("INSERT INTO QuestionChapterInfo (QuestionID,ItemBankID,ChapterID) VALUES (#{questionId},#{itemBankId},#{chapterId})")
    int insertQuestionChapterInfo(QuestionChapterInfo model);

    /**
     * 新增试题主体信息.
     *
     * @param model 试题主体
     * @return the int
     */
    //@InsertProvider(type = QuestionWriteSqlProvider.class, method = "insert")
    @Insert("INSERT INTO Question (questionId,questionTypeId,questionCategory,difficulty,title,comment,score,keywords,questionAnalysis,fieldCount,creater,createTime,status,isCheck,checker,checkTime,lastUpdateTime,displayCount,displayTime,source,isCourseOnly,inputType,importType,sourceType,parentQuestionId,isSubQuestion,sequenceIndex,isSubjective,shuffle,questionVersion,difference,perceiver,masterLevel,responseTime,questionInnerTypeId,modifier,modifyTime,mediaType,mediaDuration,isPublish,publisherId,publishTime,qualityGrade,useType) VALUES (#{questionId},#{questionTypeId},#{questionCategory},#{difficulty},#{title},#{comment},#{score},#{keywords},#{questionAnalysis},#{fieldCount},#{creater},#{createTime},#{status},#{isCheck},#{checker},#{checkTime},#{lastUpdateTime},#{displayCount},#{displayTime},#{source},#{isCourseOnly},#{inputType},#{importType},#{sourceType},#{parentQuestionId},#{isSubQuestion},#{sequenceIndex},#{isSubjective},#{shuffle},#{questionVersion},#{difference},#{perceiver},#{masterLevel},#{responseTime},#{questionInnerTypeId},#{modifier},#{modifyTime},#{mediaType},#{mediaDuration},#{isPublish},#{publisherId},#{publishTime},#{qualityGrade},#{useType})")
    int insert(Question model);

    /**
     * Update 试题主体信息.
     *
     * @param model 试题主体
     * @return the int
     */
    @Update("UPDATE Question SET Difficulty=#{difficulty},title=#{title},score=#{score},Keywords=#{keywords},QuestionAnalysis=#{questionAnalysis},FieldCount=#{fieldCount},Shuffle=#{shuffle},Perceiver=#{perceiver},MasterLevel=#{masterLevel},ResponseTime=#{responseTime},IsSubjective=#{isSubjective},Modifier=#{modifier},ModifyTime=#{modifyTime},LastUpdateTime=#{lastUpdateTime} WHERE QuestionID=#{questionId}")
    int update(Question model);

    /**
     * 更新试题审核状态.
     *
     * @param id        试题id
     * @param isCheck   审核状态
     * @param checkTime 审核时间
     * @return the int
     */
    @Update("UPDATE Question SET IsCheck=${isCheck}, CheckTime=#{checkTime} WHERE QuestionID=#{id}")
    int updateQuestionCheckStatus(@Param("id") String id, @Param("isCheck") int isCheck, @Param("checkTime") String checkTime);

    /**
     * 更新试题停启用状态.
     *
     * @param id         试题id
     * @param itemBankId 题库 id
     * @param status     停启用状态
     * @return the int
     */
    @Update("UPDATE QuestionUseRelation SET status=${status} WHERE QuestionID=#{id} AND ItemBankID=#{itemBankId}")
    int updateQuestionStatus(@Param("id") String id, @Param("itemBankId") String itemBankId, @Param("status") int status);

    /**
     * 删除question表中的数据
     *
     * @param id 试题id
     * @return int int
     */
    @Delete("DELETE FROM Question WHERE QuestionID=#{id}")
    int deleteQuestion(@Param("id") String id);

    /**
     * 删除questionContent表中的数据
     *
     * @param id 试题id
     * @return int int
     */
    @Delete("DELETE FROM QuestionContent WHERE QuestionID=#{id}")
    int deleteQuestionContent(@Param("id") String id);

    /**
     * 删除QuestionChapterInfo表中的数据
     *
     * @param id 试题id
     * @return int int
     */
    @Delete("DELETE FROM QuestionChapterInfo WHERE QuestionID=#{id}")
    int deleteQuestionChapterInfo(@Param("id") String id);

    /**
     * 删除QuestionDisplayInfo表中的数据
     *
     * @param id 试题id
     * @return int int
     */
    @Delete("DELETE FROM QuestionDisplayInfo WHERE QuestionID=#{id}")
    int deleteQuestionDisplayInfo(@Param("id") String id);

    /**
     * 删除QuestionDisplayInfo表中的数据
     *
     * @param id 试题id
     * @return int int
     */
    @Delete("DELETE FROM QuestionExendFieldValue WHERE QuestionID=#{id}")
    int deleteQuestionExendFieldValue(@Param("id") String id);

    /**
     * 删除QuestionRule表中的数据
     *
     * @param id 试题id
     * @return int int
     */
    @Delete("DELETE FROM QuestionRule WHERE QuestionID=#{id}")
    int deleteQuestionRule(@Param("id") String id);

    /**
     * 删除QuestionUseRelation表中的数据
     *
     * @param id 试题id
     * @return int int
     */
    @Delete("DELETE FROM QuestionUseRelation WHERE QuestionID=#{id}")
    int deleteQuestionUseRelation(@Param("id") String id);

    /**
     * 删除RepulsiveQuqestionID表中的数据
     *
     * @param id 试题id
     * @return int int
     */
    @Delete("DELETE FROM RepulsiveQuqestionID WHERE QuestionID=#{id}")
    int deleteRepulsiveQuqestionID(@Param("id") String id);

    /**
     * 修改试题内容.
     *
     * @param questionId 试题 id
     * @param contentXml 试题内容 xml
     */
    @Update("UPDATE QuestionContent SET ContentXml=#{contentXml} WHERE QuestionID=#{id}")
    void updateQuestionContent(@Param("id") String questionId, @Param("contentXml") String contentXml);

    /**
     * 修改试题章节.
     *
     * @param questionId 试题 id
     * @param itemBankId 题库 id
     * @param chapterId  章节 id
     */
    @Update("UPDATE QuestionChapterInfo SET ChapterID=#{chapterId} WHERE QuestionID=#{id} AND ItemBankID=#{itemBankId}")
    void updateQuestionChapterInfo(@Param("id") String questionId, @Param("itemBankId") String itemBankId, @Param("chapterId") String chapterId);

    /**
     * 批量修改试题状态.
     *
     * @param itemBankId the item bank id
     * @param status     the status
     * @param join       the join
     * @param start      the start
     * @param stop       the stop
     * @return the int
     */
    @Update("UPDATE QuestionUseRelation SET Status=${status} WHERE Status IN (${stop},${start}) AND ItemBankId=#{itemBankId} AND QuestionId IN (#{join})")
    int batchUpdateStatus(@Param("itemBankId") String itemBankId, @Param("status") int status, @Param("join") String join, @Param("start") int start, @Param("stop") int stop);

    /**
     * 将给定题库id下的试题设置为共享.
     *
     * @param itemBankId 源题库id
     * @return the int
     */
    @Update("UPDATE Question SET UseType = 1 WHERE QuestionID IN(SELECT QuestionID FROM QuestionUseRelation WHERE ItemBankID = #{itemBankId})")
    int shareQuestion(@Param("itemBankId") String itemBankId);

    /**
     * 复制试题使用关系.
     *
     * @param sourceItemBankId 源题库id
     * @param targetItemBankId 目标题库id
     * @param targetAppId      目标appid
     * @param targetOrgId      目标机构id
     * @return the int
     */
    @Insert("INSERT INTO QuestionUseRelation (QuestionID, ItemBankID, AppID, CreateOrgID) SELECT QuestionID,#{targetItemBankId},#{targetAppId},#{targetOrgId} FROM QuestionUseRelation WHERE ItemBankID = #{sourceItemBankId} ")
    int copyQuestionRelation(@Param("sourceItemBankId") String sourceItemBankId, @Param("targetItemBankId") String targetItemBankId,
                             @Param("targetAppId") String targetAppId, @Param("targetOrgId") String targetOrgId);

    /**
     * 根据题库id删除题库试题关系
     *
     * @param itemBankid 题库id
     * @return int int
     */
    @Delete("DELETE FROM QuestionUseRelation WHERE ItemBankID=#{itemBankid}")
    int deleteQuestionUseRelationByItemBank(@Param("itemBankid") String itemBankid);

    /**
     * 修改试题更新时间
     * @param id 试题ID
     * @param lastUpdateTime 最后更新时间
     */
    @Update("UPDATE Question SET LastUpdateTime=#{lastUpdateTime} WHERE QuestionID=#{id}")
    void updateLastUpdateTime(@Param("id") String id, @Param("lastUpdateTime") String lastUpdateTime);
}


